/*
 * 补发单新建
 * */
var eventId = "";
var btnName = "";
var shopId = "";
var projectId = "";
var tblCheckOrder = null;

$(document).ready(function() {
	var btnName = "";
	// // 日期控件初始化，并且隐藏
	$('#modelBody .datetimepicker-default').datetimepicker();
	eventId = extractEventId();
	btnName = extractBtnText();
	projectId = extractprojectId();
	initOrderDataTable("", "");
	$("#tblCheckOrder").tooltip({
        selector: ".tooltip-column-detail",
        container: "body",
        title: function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });

});

// 根据项目ID获取订单
function initOrderDataTable(buyerNick, tradeCode) {
	tblCheckOrder = $("#tblCheckOrder").DataTable({
	    "processing" : true,
	    "serverSide" : true,
	    "lengthMenu" : [ [ 10, 25, 50, -1 ], [ 10, 25, 50, "All" ] ],
	    "dom" : '<"bottom"rtflp>',
	    "searching" : false,
	    "scrollX" : "100%",
	    "scrollXInner" : "110%",
	    "scrollY":"390px",
	    "bPaginate" : false, // 是否显示（应用）分页器
	    "bInfo" : false, // 是否显示页脚信息，DataTables插件左下角显示记录数
	    "pagingType" : "full_numbers",
	    "deferRender" : true,
	    "ajax" : {
	        "url" : "/order/getOrderForEvent.action",
	        "data" : function(d) {
		        d.buyerNick = encodeURI(buyerNick);
		        d.tradeCode = encodeURI(tradeCode);
	        }
	    },
	    "tableTools" : {
		    "sRowSelect" : "bootstrap"
	    },"columnDefs" : [{
        	"className" : "truncate-text tooltip-column-detail",
            "targets" : [ 0 ]
        },{
        	"className" : "truncate-text tooltip-column-detail",
            "targets" : [ 1 ]
        },{
        	"className" : "truncate-text tooltip-column-detail",
            "targets" : [ 2 ]
        },{
        	"className" : "truncate-text tooltip-column-detail",
            "targets" : [ 3 ]
        },{
        	"className" : "truncate-text tooltip-column-detail",
            "targets" : [ 4 ]
        },{
        	"className" : "truncate-text tooltip-column-detail",
            "targets" : [ 5 ]
        }],
	    "columns" : [ {
	        "data" : "orderCode",
	        "orderable" : false
	    }, {
	        "data" : "tradeCode",
	        "orderable" : false
	    },  {
	        "data" : "buyerNick",
	        "orderable" : false
	    }, {
	        "data" : "paidDate",
	        "orderable" : false
	    }, {
	        "data" : "shopName",
	        "orderable" : false
	    },{
	        "data" : "statusAfterSale",
	        "orderable" : false
	    }]
	});
}
function clearDataTable() {
	var dttable;
	if ($('#tblCheckOrder').hasClass('dataTable')) {
		dttable = $('#tblCheckOrder').dataTable();
		dttable.fnDestroy(); // 还原初始化了的datatable
		dttable.fnClearTable(); // 清空一下table
	}
}

$("#selectOrderList").on('click', function() {
	var buyerNick = $("#buyerNick").val();
	var tradeCode = $("#ecTradeCode").val();
	clearDataTable();
	initOrderDataTable(buyerNick, tradeCode)
});
$("#selectOrderListClear").on('click',function(){
	$("#buyerNick").val("");
	$("#ecTradeCode").val("");
})

// 获取datatable中选中的一行数据
$('#tblCheckOrder tbody').on('click', 'tr', function() {
	var orderCode = tblCheckOrder.row(this).data().orderCode;// 来源单号
	var tradeCode = tblCheckOrder.row(this).data().tradeCode;// 平台交易号
	var buyerNick = tblCheckOrder.row(this).data().buyerNick;// 会员ID
	var orderId = tblCheckOrder.row(this).data().orderId;// 会员ID
	console.log(orderCode);
	console.log(tradeCode);
	console.log(orderId);
	$('#orderCode', parent.document).val(orderCode);
	$('#tradeCode', parent.document).val(tradeCode);
	$('#buyerNick', parent.document).val(buyerNick);
	$('#orderID', parent.document).val(orderId);
//	$('#eventCheckWindow', window.top.document).find("#btnEventOrderClose").click()
	parent.getshiporderCodeList(orderId);
	$('#btnEventOrderClose', parent.document).click();
});

// 从url中解析按钮名称
function extractBtnText() {
	return extractParam("btnType");
};
// resendId
function extractEventId() {
	return extractParam("eventId");
};
// projectId
function extractprojectId() {
	return extractParam("projectId");
};
// ===========公共方法==========

// 获取页面参数
function GetArgsFromHref(sHref, sArgName) {
	var args = sHref.split("?");
	var retval = "";

	if (args[0] == sHref) /* 参数为空 */
	{
		return retval; /* 无需做任何处理 */
	}
	var str = args[1];
	args = str.split("&");
	for (var i = 0; i < args.length; i++) {
		str = args[i];
		var arg = str.split("=");
		if (arg.length <= 1)
			continue;
		if (arg[0] == sArgName)
			retval = arg[1];
	}
	return retval;
}

function extractParam(paramName) {
	var returnString = "";
	var str = window.location.search;
	if (!!str) {
		returnString = GetArgsFromHref(str, paramName);
	}
	return returnString;
}
